package com.etcn.modules.statical.statistics.service.impl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.etcn.common.annotation.QueryCache;
import com.etcn.common.config.InterfaceConfig;
import com.etcn.common.utils.DateUtil;
import com.etcn.common.utils.PubFun;
import com.etcn.common.utils.StringUtil;
import com.etcn.common.utils.UserUtil;
import com.etcn.modules.busi.member.pojo.WmMemberAttention;
import com.etcn.modules.busi.member.service.IWmMemberAttentionService;
import com.etcn.modules.statical.base.pojo.BaseCompany;
import com.etcn.modules.statical.base.pojo.BaseCountry;
import com.etcn.modules.statical.base.service.IBaseAreaService;
import com.etcn.modules.statical.base.service.IBaseCompanyService;
import com.etcn.modules.statical.base.service.IBaseCountryService;
import com.etcn.modules.statical.base.service.IBaseGoodsService;
import com.etcn.modules.statical.exponent.pojo.WmAreaCountryRate;
import com.etcn.modules.statical.exponent.pojo.WmAreaExponentStatistics;
import com.etcn.modules.statical.exponent.pojo.WmAreaGoodsRate;
import com.etcn.modules.statical.exponent.pojo.WmAreaTradeWayRate;
import com.etcn.modules.statical.exponent.pojo.WmAreaTransportWayRate;
import com.etcn.modules.statical.exponent.pojo.WmCompanyCountryRate;
import com.etcn.modules.statical.exponent.pojo.WmCompanyExponentStatistics;
import com.etcn.modules.statical.exponent.pojo.WmCompanyGoodsRate;
import com.etcn.modules.statical.exponent.pojo.WmCompanyTradeWayRate;
import com.etcn.modules.statical.exponent.pojo.WmCompanyTransportWayRate;
import com.etcn.modules.statical.exponent.pojo.WmCountryExponentStatistics;
import com.etcn.modules.statical.exponent.pojo.WmCountryGoodsRate;
import com.etcn.modules.statical.exponent.pojo.WmCountryTradeAreaRate;
import com.etcn.modules.statical.exponent.pojo.WmCountryTradeWayRate;
import com.etcn.modules.statical.exponent.pojo.WmCountryTransportWayRate;
import com.etcn.modules.statical.exponent.pojo.WmGoodsCountryRate;
import com.etcn.modules.statical.exponent.pojo.WmGoodsExponentStatistics;
import com.etcn.modules.statical.exponent.pojo.WmGoodsTradeAreaRate;
import com.etcn.modules.statical.exponent.pojo.WmGoodsTradeWayRate;
import com.etcn.modules.statical.exponent.pojo.WmGoodsTransportWayRate;
import com.etcn.modules.statical.exponent.service.IWmAreaCountryRateService;
import com.etcn.modules.statical.exponent.service.IWmAreaExponentStatisticsService;
import com.etcn.modules.statical.exponent.service.IWmAreaGoodsRateService;
import com.etcn.modules.statical.exponent.service.IWmAreaTradeWayRateService;
import com.etcn.modules.statical.exponent.service.IWmAreaTransportWayRateService;
import com.etcn.modules.statical.exponent.service.IWmCompanyCountryRateService;
import com.etcn.modules.statical.exponent.service.IWmCompanyExponentStatisticsService;
import com.etcn.modules.statical.exponent.service.IWmCompanyGoodsRateService;
import com.etcn.modules.statical.exponent.service.IWmCompanyTradeWayRateService;
import com.etcn.modules.statical.exponent.service.IWmCompanyTransportWayRateService;
import com.etcn.modules.statical.exponent.service.IWmCountryExponentStatisticsService;
import com.etcn.modules.statical.exponent.service.IWmCountryGoodsRateService;
import com.etcn.modules.statical.exponent.service.IWmCountryTradeAreaRateService;
import com.etcn.modules.statical.exponent.service.IWmCountryTradeWayRateService;
import com.etcn.modules.statical.exponent.service.IWmCountryTransportWayRateService;
import com.etcn.modules.statical.exponent.service.IWmGoodsCountryRateService;
import com.etcn.modules.statical.exponent.service.IWmGoodsExponentStatisticsService;
import com.etcn.modules.statical.exponent.service.IWmGoodsTradeAreaRateService;
import com.etcn.modules.statical.exponent.service.IWmGoodsTradeWayRateService;
import com.etcn.modules.statical.exponent.service.IWmGoodsTransportWayRateService;
import com.etcn.modules.statical.statistics.pojo.Adas;
import com.etcn.modules.statical.statistics.pojo.Base;
import com.etcn.modules.statical.statistics.pojo.Ccc;
import com.etcn.modules.statical.statistics.pojo.Classifydecide;
import com.etcn.modules.statical.statistics.pojo.ExportAmount;
import com.etcn.modules.statical.statistics.pojo.Quarantine;
import com.etcn.modules.statical.statistics.pojo.RegulatoryCondition;
import com.etcn.modules.statical.statistics.pojo.Statistics;
import com.etcn.modules.statical.statistics.pojo.Tax;
import com.etcn.modules.statical.statistics.service.IStatisticsService;

@Service("statisticsService")
public class StatisticsServiceImpl implements IStatisticsService {

	@Autowired
	private IWmGoodsExponentStatisticsService wmGoodsExponentStatisticsService;
	@Autowired
	private IWmCountryExponentStatisticsService wmCountryExponentStatisticsService;
	@Autowired
	private IWmCompanyExponentStatisticsService wmCompanyExponentStatisticsService;
	@Autowired
	private IWmAreaExponentStatisticsService wmAreaExponentStatisticsService;
	@Autowired
	private IWmGoodsCountryRateService wsmGoodsCountryRateService;
	@Autowired
	private IWmGoodsTradeAreaRateService wmGoodsTradeAreaRateService;
	@Autowired
	private IWmGoodsTradeWayRateService wmGoodsTradeWayRateService;
	@Autowired
	private IWmGoodsTransportWayRateService wmGoodsTransportWayRateService;

	@Autowired
	private IWmCountryTransportWayRateService wmCountryTransportWayRateService;
	@Autowired
	private IWmCountryTradeWayRateService wmCountryTradeWayRateService;
	@Autowired
	private IWmCountryTradeAreaRateService wmCountryTradeAreaRateService;
	@Autowired
	private IWmCountryGoodsRateService wmCountryGoodsRateService;

	@Autowired
	private IWmCompanyTransportWayRateService wmCompanyTransportWayRateService;
	@Autowired
	private IWmCompanyTradeWayRateService wmCompanyTradeWayRateService;
	@Autowired
	private IWmCompanyGoodsRateService wmCompanyGoodsRateService;
	@Autowired
	private IWmCompanyCountryRateService wmCompanyCountryRateService;

	@Autowired
	private IWmAreaTransportWayRateService wmAreaTransportWayRateService;
	@Autowired
	private IWmAreaTradeWayRateService wmAreaTradeWayRateService;
	@Autowired
	private IWmAreaGoodsRateService wmAreaGoodsRateService;
	@Autowired
	private IWmAreaCountryRateService wmAreaCountryRateService;

	@Autowired
	private IBaseCountryService baseCountryService;
	@Autowired
	private IBaseAreaService baseAreaService;
	@Autowired
	private IBaseCompanyService baseCompanyService;
	@Autowired
	private IBaseGoodsService baseGoodsService;

	@Autowired
	private IWmMemberAttentionService wmMemberAttentionService;

	@Autowired
	private InterfaceConfig interfaceConfig;

	@QueryCache
	@Override
	public Map<String, Object> getExponent(Statistics entity) throws Exception {

		Map<String, Object> retMap = new HashMap<String, Object>();
		List<Object> importTradeTypeDatas = new ArrayList<Object>();
		List<Object> exportTradeTypeDatas = new ArrayList<Object>();
		Map<String, Object> exponents = new HashMap<String, Object>();
		exponents.put("import", importTradeTypeDatas);
		exponents.put("export", exportTradeTypeDatas);
		retMap.put("exponents", exponents);
		String type = entity.getType();
		// 统计年份
		String year = entity.getYear();
		if (StringUtils.isEmpty(year)) {
			year = DateUtil.getCurrentYearStr();
		}
		// 统计日期类型
		String statisticDatetype = "1";
		if ("country".equals(type)) {
			WmCountryExponentStatistics param = new WmCountryExponentStatistics();
			param.setCountryCode(entity.getCode());
			param.setStatisticDatetype(statisticDatetype);
			param.setYear(year);
			List<WmCountryExponentStatistics> lcountrys = wmCountryExponentStatisticsService.findListByCountry(param);
			for (WmCountryExponentStatistics wmCountryExponentStatistics : lcountrys) {
				classifyByTradeType(wmCountryExponentStatistics.getTradeType(), wmCountryExponentStatistics,
						importTradeTypeDatas, exportTradeTypeDatas);
			}
		} else if ("area".equals(type)) {
			WmAreaExponentStatistics param = new WmAreaExponentStatistics();
			String code = entity.getCode();
			if (code.length() == 4) {
				code = code.substring(0, 2) + code;
			}
			param.setAreaCode(code);
			param.setStatisticDatetype(statisticDatetype);
			param.setYear(year);
			List<WmAreaExponentStatistics> lareas = wmAreaExponentStatisticsService.findListByArea(param);
			for (WmAreaExponentStatistics wmAreaExponentStatistics : lareas) {
				classifyByTradeType(wmAreaExponentStatistics.getTradeType(), wmAreaExponentStatistics,
						importTradeTypeDatas, exportTradeTypeDatas);
			}
		} else if ("company".equals(type)) {
			WmCompanyExponentStatistics param = new WmCompanyExponentStatistics();
			param.setCompanyCode(entity.getCode());
			param.setStatisticDatetype(statisticDatetype);
			param.setYear(year);
			List<WmCompanyExponentStatistics> lcompanys = wmCompanyExponentStatisticsService.findListByCompany(param);
			for (WmCompanyExponentStatistics wmCompanyExponentStatistics : lcompanys) {
				classifyByTradeType(wmCompanyExponentStatistics.getTradeType(), wmCompanyExponentStatistics,
						importTradeTypeDatas, exportTradeTypeDatas);
			}
		} else if ("goods".equals(type)) {
			WmGoodsExponentStatistics param = new WmGoodsExponentStatistics();
			param.setHscode(entity.getCode());
			param.setStatisticDatetype(statisticDatetype);
			param.setYear(year);
			List<WmGoodsExponentStatistics> lgoodses = wmGoodsExponentStatisticsService.findListByGoods(param);
			for (WmGoodsExponentStatistics wmGoodsExponentStatistics : lgoodses) {
				classifyByTradeType(wmGoodsExponentStatistics.getTradeType(), wmGoodsExponentStatistics,
						importTradeTypeDatas, exportTradeTypeDatas);
			}
		}
		return retMap;
	}

	/**
	 * 按照贸易类型分类
	 * 
	 * @param tradeType
	 *            贸易类型
	 * @param o
	 *            指数或占比详情
	 * @param importTradeTypeDatas
	 *            进口数据
	 * @param exportTradeTypeDatas
	 *            出口数据
	 * @throws Exception
	 */
	private void classifyByTradeType(String tradeType, Object o, List<Object> importTradeTypeDatas,
			List<Object> exportTradeTypeDatas) throws Exception {

		if ("1".equals(tradeType)) {
			importTradeTypeDatas.add(o);
		} else {
			exportTradeTypeDatas.add(o);
		}
	}

	@QueryCache
	@Override
	public Map<String, Object> getRate(Statistics entity) throws Exception {

		Map<String, Object> retMap = new HashMap<String, Object>();
		String type = entity.getType();
		String statisticDatetype = entity.getStatisticDatetype();
		statisticDatetype = (StringUtils.isEmpty(statisticDatetype)) ? "4" : statisticDatetype;
		String statisticValue = entity.getStatisticValue();
		statisticValue = (StringUtils.isEmpty(statisticValue)) ? DateUtil.getCurrentYearStr() : statisticValue;
		String statisticType = entity.getStatisticType();
		statisticType = (StringUtils.isEmpty(statisticType)) ? "1" : statisticType;
		// 统计年份
		String year = entity.getYear();
		if (StringUtils.isEmpty(year)) {
			year = DateUtil.getCurrentYearStr();
		}
		if ("country".equals(type)) {
			// 贸易地区信息
			WmCountryTradeAreaRate wmCountryTradeAreaRate = new WmCountryTradeAreaRate();
			wmCountryTradeAreaRate.setCountryCode(entity.getCode());
			wmCountryTradeAreaRate.setYear(year);
			wmCountryTradeAreaRate.setStatisticDatetype(statisticDatetype);
			wmCountryTradeAreaRate.setStatisticValue(statisticValue);
			wmCountryTradeAreaRate.setStatisticType(statisticType);
			List<WmCountryTradeAreaRate> tradeAreaRate = wmCountryTradeAreaRateService
					.getListByCountry(wmCountryTradeAreaRate);
			retMap.put("tradeAreaRate", tradeAreaRate);
			// 商品占比
			WmCountryGoodsRate wmCountryGoodsRate = new WmCountryGoodsRate();
			wmCountryGoodsRate.setCountryCode(entity.getCode());
			wmCountryGoodsRate.setYear(year);
			wmCountryGoodsRate.setStatisticDatetype(statisticDatetype);
			wmCountryGoodsRate.setStatisticValue(statisticValue);
			wmCountryGoodsRate.setStatisticType(statisticType);
			List<WmCountryGoodsRate> goodsRate = wmCountryGoodsRateService.findListByCountry(wmCountryGoodsRate);
			retMap.put("goodsRate", goodsRate);
			// 贸易方式信息
			WmCountryTradeWayRate wmCountryTradeWayRate = new WmCountryTradeWayRate();
			wmCountryTradeWayRate.setCountryCode(entity.getCode());
			wmCountryTradeWayRate.setYear(year);
			wmCountryTradeWayRate.setStatisticDatetype(statisticDatetype);
			wmCountryTradeWayRate.setStatisticValue(statisticValue);
			wmCountryTradeWayRate.setStatisticType(statisticType);
			List<WmCountryTradeWayRate> tradeWayRate = wmCountryTradeWayRateService
					.getListByCountry(wmCountryTradeWayRate);
			retMap.put("tradeWayRate", tradeWayRate);
			// 运输方式
			WmCountryTransportWayRate wmCountryTransportWayRate = new WmCountryTransportWayRate();
			wmCountryTransportWayRate.setCountryCode(entity.getCode());
			wmCountryTransportWayRate.setYear(year);
			wmCountryTransportWayRate.setStatisticDatetype(statisticDatetype);
			wmCountryTransportWayRate.setStatisticValue(statisticValue);
			wmCountryTransportWayRate.setStatisticType(statisticType);
			List<WmCountryTransportWayRate> transportWayRate = wmCountryTransportWayRateService
					.getListByCountry(wmCountryTransportWayRate);
			retMap.put("transportWayRate", transportWayRate);
		} else if ("area".equals(type)) {
			String code = entity.getCode();
			if (code.length() == 4) {
				code = code.substring(0, 2) + code;
			}
			// 目的国信息
			WmAreaCountryRate wmAreaCountryRate = new WmAreaCountryRate();
			wmAreaCountryRate.setAreaCode(code);
			wmAreaCountryRate.setYear(year);
			wmAreaCountryRate.setStatisticDatetype(statisticDatetype);
			wmAreaCountryRate.setStatisticValue(statisticValue);
			wmAreaCountryRate.setStatisticType(statisticType);
			List<WmAreaCountryRate> destCountryRate = wmAreaCountryRateService.getListByArea(wmAreaCountryRate);
			retMap.put("destCountryRate", destCountryRate);
			// 商品占比
			WmAreaGoodsRate wmAreaGoodsRate = new WmAreaGoodsRate();
			wmAreaGoodsRate.setAreaCode(code);
			wmAreaGoodsRate.setYear(year);
			wmAreaGoodsRate.setStatisticDatetype(statisticDatetype);
			wmAreaGoodsRate.setStatisticValue(statisticValue);
			wmAreaGoodsRate.setStatisticType(statisticType);
			List<WmAreaGoodsRate> goodsRate = wmAreaGoodsRateService.findListByArea(wmAreaGoodsRate);
			retMap.put("goodsRate", goodsRate);
			// 贸易方式信息
			WmAreaTradeWayRate wmAreaTradeWayRate = new WmAreaTradeWayRate();
			wmAreaTradeWayRate.setAreaCode(code);
			wmAreaTradeWayRate.setYear(year);
			wmAreaTradeWayRate.setStatisticDatetype(statisticDatetype);
			wmAreaTradeWayRate.setStatisticValue(statisticValue);
			wmAreaTradeWayRate.setStatisticType(statisticType);
			List<WmAreaTradeWayRate> tradeWayRate = wmAreaTradeWayRateService.getListByArea(wmAreaTradeWayRate);
			retMap.put("tradeWayRate", tradeWayRate);
			// 运输方式
			WmAreaTransportWayRate wmAreaTransportWayRate = new WmAreaTransportWayRate();
			wmAreaTransportWayRate.setAreaCode(code);
			wmAreaTransportWayRate.setYear(year);
			wmAreaTransportWayRate.setStatisticDatetype(statisticDatetype);
			wmAreaTransportWayRate.setStatisticValue(statisticValue);
			wmAreaTransportWayRate.setStatisticType(statisticType);
			List<WmAreaTransportWayRate> transportWayRate = wmAreaTransportWayRateService
					.getListByArea(wmAreaTransportWayRate);
			retMap.put("transportWayRate", transportWayRate);
		} else if ("company".equals(type)) {
			// 目的国信息
			WmCompanyCountryRate wmCompanyCountryRate = new WmCompanyCountryRate();
			wmCompanyCountryRate.setCompanyCode(entity.getCode());
			wmCompanyCountryRate.setYear(year);
			wmCompanyCountryRate.setStatisticDatetype(statisticDatetype);
			wmCompanyCountryRate.setStatisticValue(statisticValue);
			wmCompanyCountryRate.setStatisticType(statisticType);
			List<WmCompanyCountryRate> destCountryRate = wmCompanyCountryRateService
					.getListByCompany(wmCompanyCountryRate);
			retMap.put("destCountryRate", destCountryRate);
			// 商品占比
			WmCompanyGoodsRate wmCompanyGoodsRate = new WmCompanyGoodsRate();
			wmCompanyGoodsRate.setCompanyCode(entity.getCode());
			wmCompanyGoodsRate.setYear(year);
			wmCompanyGoodsRate.setStatisticDatetype(statisticDatetype);
			wmCompanyGoodsRate.setStatisticValue(statisticValue);
			wmCompanyGoodsRate.setStatisticType(statisticType);
			List<WmCompanyGoodsRate> goodsRate = wmCompanyGoodsRateService.findListByCompany(wmCompanyGoodsRate);
			retMap.put("goodsRate", goodsRate);
			// 贸易方式信息
			WmCompanyTradeWayRate wmCompanyTradeWayRate = new WmCompanyTradeWayRate();
			wmCompanyTradeWayRate.setCompanyCode(entity.getCode());
			wmCompanyTradeWayRate.setYear(year);
			wmCompanyTradeWayRate.setStatisticDatetype(statisticDatetype);
			wmCompanyTradeWayRate.setStatisticValue(statisticValue);
			wmCompanyTradeWayRate.setStatisticType(statisticType);
			List<WmCompanyTradeWayRate> tradeWayRate = wmCompanyTradeWayRateService
					.getListByCompany(wmCompanyTradeWayRate);
			retMap.put("tradeWayRate", tradeWayRate);
			// 运输方式
			WmCompanyTransportWayRate wmCompanyTransportWayRate = new WmCompanyTransportWayRate();
			wmCompanyTransportWayRate.setCompanyCode(entity.getCode());
			wmCompanyTransportWayRate.setYear(year);
			wmCompanyTransportWayRate.setStatisticDatetype(statisticDatetype);
			wmCompanyTransportWayRate.setStatisticValue(statisticValue);
			wmCompanyTransportWayRate.setStatisticType(statisticType);
			List<WmCompanyTransportWayRate> transportWayRate = wmCompanyTransportWayRateService
					.getListByCompany(wmCompanyTransportWayRate);
			retMap.put("transportWayRate", transportWayRate);
		} else if ("goods".equals(type)) {
			// 目的国信息
			WmGoodsCountryRate wmGoodsCountryRate = new WmGoodsCountryRate();
			wmGoodsCountryRate.setHscode(entity.getCode());
			wmGoodsCountryRate.setYear(year);
			wmGoodsCountryRate.setStatisticDatetype(statisticDatetype);
			wmGoodsCountryRate.setStatisticValue(statisticValue);
			wmGoodsCountryRate.setStatisticType(statisticType);
			List<WmGoodsCountryRate> destCountryRate = wsmGoodsCountryRateService.getListByGoods(wmGoodsCountryRate);
			retMap.put("destCountryRate", destCountryRate);
			// 贸易地区信息
			WmGoodsTradeAreaRate wmGoodsTradeAreaRate = new WmGoodsTradeAreaRate();
			wmGoodsTradeAreaRate.setHscode(entity.getCode());
			wmGoodsTradeAreaRate.setYear(year);
			wmGoodsTradeAreaRate.setStatisticDatetype(statisticDatetype);
			wmGoodsTradeAreaRate.setStatisticValue(statisticValue);
			wmGoodsTradeAreaRate.setStatisticType(statisticType);
			List<WmGoodsTradeAreaRate> tradeAreaRate = wmGoodsTradeAreaRateService.getListByGoods(wmGoodsTradeAreaRate);
			retMap.put("tradeAreaRate", tradeAreaRate);
			// 贸易方式信息
			WmGoodsTradeWayRate wmGoodsTradeWayRate = new WmGoodsTradeWayRate();
			wmGoodsTradeWayRate.setHscode(entity.getCode());
			wmGoodsTradeWayRate.setYear(year);
			wmGoodsTradeWayRate.setStatisticDatetype(statisticDatetype);
			wmGoodsTradeWayRate.setStatisticValue(statisticValue);
			wmGoodsTradeWayRate.setStatisticType(statisticType);
			List<WmGoodsTradeWayRate> tradeWayRate = wmGoodsTradeWayRateService.getListByGoods(wmGoodsTradeWayRate);
			retMap.put("tradeWayRate", tradeWayRate);
			// 运输方式
			WmGoodsTransportWayRate wmGoodsTransportWayRate = new WmGoodsTransportWayRate();
			wmGoodsTransportWayRate.setHscode(entity.getCode());
			wmGoodsTransportWayRate.setYear(year);
			wmGoodsTransportWayRate.setStatisticDatetype(statisticDatetype);
			wmGoodsTransportWayRate.setStatisticValue(statisticValue);
			wmGoodsTransportWayRate.setStatisticType(statisticType);
			List<WmGoodsTransportWayRate> transportWayRate = wmGoodsTransportWayRateService
					.getListByGoods(wmGoodsTransportWayRate);
			retMap.put("transportWayRate", transportWayRate);
		}

		List<String> keys = new ArrayList<>(retMap.keySet());
		for (String key : keys) {
			Object v = retMap.get(key);
			if (v != null) {
				List<Object> vl = (List<Object>) v;
				List<Object> importTradeTypeDatas = new ArrayList<Object>();
				List<Object> exportTradeTypeDatas = new ArrayList<Object>();
				for (Object o : vl) {
					String tradeType = (String) getValue(o, "tradeType");
					if ("1".equals(tradeType)) {
						importTradeTypeDatas.add(o);
					} else {
						exportTradeTypeDatas.add(o);
					}
				}
				Map<String, Object> vm = new HashMap<String, Object>();
				vm.put("import", importTradeTypeDatas);
				vm.put("export", exportTradeTypeDatas);
				retMap.put(key, vm);
			}
		}
		return retMap;
	}

	private Object getValue(Object dto, String name) throws Exception {

		Method[] m = dto.getClass().getMethods();
		for (int i = 0; i < m.length; i++) {
			if (("get" + name).toLowerCase().equals(m[i].getName().toLowerCase())) {
				return m[i].invoke(dto);
			}
		}
		return null;
	}

	@QueryCache
	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getDetail(Statistics entity) throws Exception {

		Map<String, Object> retMap = new HashMap<String, Object>();
		String type = entity.getType();
		String typeNum = null;
		String code = entity.getCode();
		// 获取指数信息
		Map<String, Object> exponentMap = getExponent(entity);
		retMap.putAll(exponentMap);
		// 获取基础信息
		if ("country".equals(type)) {
			retMap.put("base", baseCountryService.getByCode(code));
			typeNum = "1";
		} else if ("area".equals(type)) {
			retMap.put("base", baseAreaService.getByCode(code));
			typeNum = "2";
		} else if ("company".equals(type)) {
			BaseCompany company = baseCompanyService.getByCode(code);
			// 获取企业类型大类
			Map<String, Object> exponents = (Map<String, Object>)exponentMap.get("exponents");
			List<Object> importTradeTypeDatas = (List<Object>)exponents.get("import");
			List<Object> exportTradeTypeDatas = (List<Object>)exponents.get("export");
			String typeName = "";
			if (importTradeTypeDatas.size() > 0 && exportTradeTypeDatas.size() > 0) {
				typeName = "进出口企业";
			} else if (importTradeTypeDatas.size() > 0) {
				typeName = "进口企业";
			} else if (exportTradeTypeDatas.size() > 0) {
				typeName = "出口企业";
			}
			company.setTypeName(typeName);
			retMap.put("base", company);
			typeNum = "3";
		} else if ("goods".equals(type)) {
			retMap.put("base", baseGoodsService.getByCcode(code));
			typeNum = "4";
		}
		// 获取占比信息
		entity.setStatisticType("1");
		retMap.putAll(getRate(entity));
		// 判断是否关注过
		WmMemberAttention att = new WmMemberAttention();
		att.setType(typeNum);
		att.setTypeCode(code);
		att.setMemberId(UserUtil.getCurrentMemberId());
        if(wmMemberAttentionService.judgeIfAttention(att)){
			retMap.put("attention", "1");
		} else {
			retMap.put("attention", "0");
		}

		return retMap;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, Object> getClassificationsTax(String hscode, String countryCode) throws Exception {

		Map<String, Object> viewMap = new HashMap<String, Object>();
		Map<String, Object> taxMap = PubFun.requestInterface(interfaceConfig.getConfig("query_tax") + "?language=0&hs=" + hscode
				+ "&countrycode=" + countryCode + "&exprate=1", Map.class);
		Tax tax = new Tax();
		if (taxMap != null) {
			tax.setMostFavNationsTax((String) taxMap.get("MFN"));
			tax.setGeneralTax((String) taxMap.get("Gen"));
			tax.setAddTax((String) taxMap.get("VAT"));
			tax.setSpecialTax((String) taxMap.get("MFN"));
			tax.setExportTax((String) taxMap.get("exprate01"));
		}
		viewMap.put("tax", tax);
		return viewMap;
	}

	@QueryCache
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map<String, Object> getClassificationsDetail(String hscode) throws Exception {

		Map<String, Object> viewMap = new HashMap<String, Object>();
		// 归类信息
		Base base = new Base();
		viewMap.put("base", base);
		base.setSection("第" + hscode.substring(0, 2) + "章");
		List<Map<String, Object>> classifications = PubFun.requestInterface(
				interfaceConfig.getConfig("query_classifications") + "?key=" + hscode, List.class);
		List<String> relatedProducts = new ArrayList<String>();// 相关产品
		String declareelement = "";// 申报要素
		if (classifications != null) {
			for (Map<String, Object> cls : classifications) {
				List<Map<String, Object>> contents = (List) cls.get("content");
				if (contents != null) {
					for (Map<String, Object> cont : contents) {
						if (hscode != null && hscode.equals(cont.get("HS"))) {
							base.setHscode((String) cont.get("HS"));
							base.setName((String) cont.get("Product_name"));
							String productlistcn = (String) cont.get("productlist_cn");
							if (StringUtil.isNotEmpty(productlistcn)) {
								relatedProducts = new ArrayList<String>(Arrays.asList(productlistcn.split(";")));
							}
							declareelement = (String) cont.get("dec_elements_cn");
						}
					}
				}
			}
		}

		// 重点产品
		Map<String, Object> importanthsGoodsMap = PubFun.requestInterface(
				interfaceConfig.getConfig("query_importanths_goods") + "?hs=" + hscode, Map.class);
		if (importanthsGoodsMap != null) {
			String importanthsGoods = (String) importanthsGoodsMap.get("keyname");
			if (StringUtil.isNotEmpty(importanthsGoods)) {
				relatedProducts.add(importanthsGoods);
			}
		}

		// 相关产品
		viewMap.put("relatedProducts", relatedProducts);

		// 申报要素
		viewMap.put("declareelement", declareelement == null ? "" : declareelement.split(";"));

		// 税则信息
		List<String> ret = new ArrayList<String>();
		viewMap.put("tariffs", ret);
		List<Map<String, Object>> tariffs = null;
		int hslength = 0;
		if (hscode.length() == 4 || hscode.length() == 6) {
			hslength = 4;
			tariffs = PubFun.requestInterface(
					interfaceConfig.getConfig("query_tariffs") + "?lang=0&code=" + hscode + "&nochapter=2", List.class);
		} else {
			hslength = 8;
			tariffs = PubFun.requestInterface(
					interfaceConfig.getConfig("query_tariffs") + "?lang=0&code=" + hscode + "&nochapter=3", List.class);
		}
		if (tariffs != null && tariffs.size() > 0) {
			getDir(hscode, ret, tariffs.get(0), hslength);
		}
		if (hslength == 4 && ret.size() > 0) {
			ret.remove(0);
		}

		// 归类决定
		Classifydecide classifydecide = new Classifydecide();
		viewMap.put("classifydecide", classifydecide);
		List<Map<String, Object>> classdetermines = PubFun.requestInterface(
				interfaceConfig.getConfig("query_classdetermine") + "?hs=" + hscode, List.class);
		if (classdetermines != null && classdetermines.size() > 0) {
			Map<String, Object> classdetermine = classdetermines.get(0);
			classifydecide.setHscode(hscode);
			classifydecide.setGoodsDesc((String) classdetermine.get("desc"));
			classifydecide.setNo((String) classdetermine.get("code"));
		}

		// 关税协定/特惠国
		List<BaseCountry> countrys = baseCountryService.findSimpleList();
		// 关税查询
		Tax tax = new Tax();
		viewMap.put("tax", tax);
		tax.setSpecialFavCountrys(countrys);
		Map<String, Object> taxMap = PubFun.requestInterface(interfaceConfig.getConfig("query_tax") + "?language=0&hs=" + hscode
				+ "&countrycode=" + countrys.get(0).getCode() + "&exprate=1", Map.class);
		if (taxMap != null) {
			tax.setMostFavNationsTax((String) taxMap.get("MFN"));
			tax.setGeneralTax((String) taxMap.get("Gen"));
			tax.setAddTax((String) taxMap.get("VAT"));
			tax.setSpecialTax((String) taxMap.get("MFN"));
			tax.setExportTax((String) taxMap.get("ED"));
		}

		// 反倾销列表
		List<Adas> adases = new ArrayList<Adas>();
		viewMap.put("adas", adases);
		List<Map<String, String>> adasList = PubFun.requestInterface(interfaceConfig.getConfig("query_adas") + "?language=0&hs=" + hscode, List.class);
		for (Map<String, String> adasm : adasList) {
			Adas adas = new Adas();
			adas.setCountry(adasm.get("country"));
			adas.setCompanyName(adasm.get("company_name"));
			adas.setAntiDumping(adasm.get("anti_dumping"));
			adas.setAntiSubsidy(adasm.get("anti_subsidy"));
			adas.setDumpingExpiredDate(adasm.get("dumping_expired"));
			adas.setSubsidyExpiredDate(adasm.get("subsidy_expired"));
			adases.add(adas);
		}

		// 监管条件(接口报错，暂时用假数据！！)
		RegulatoryCondition regulatoryCondition = new RegulatoryCondition();
		Map<String, Object> regulatoryConditionMap = PubFun.requestInterface(
				interfaceConfig.getConfig("query_hssupervisions") + "?language=0&hs=" + hscode, Map.class);
		if (regulatoryConditionMap != null && regulatoryConditionMap.get("impsupervisions") != null) {
			List<Map<String, Object>> impsupervisions = (List) regulatoryConditionMap.get("impsupervisions");
			if (impsupervisions.size() > 0) {
				Map<String, Object> impsupervision = impsupervisions.get(0);
				regulatoryCondition.setRegulatoryCode((String) impsupervision.get("code"));
				regulatoryCondition.setCredRequire((String) impsupervision.get("certname"));
				regulatoryCondition.setDealAgency((String) impsupervision.get("institution"));
			}
		}

		// 检验检疫
		Quarantine quarantine = new Quarantine();
		Map<String, Object> inspectionqueMap = PubFun.requestInterface(
				interfaceConfig.getConfig("query_inspectionque") + "?language=0&hs=" + hscode, Map.class);
		if (inspectionqueMap != null && inspectionqueMap.get("resultList") != null) {
			List<Map<String, Object>> inspectionques = (List<Map<String, Object>>) inspectionqueMap.get("resultList");
			if (inspectionques != null && inspectionques.size() > 0) {
				Map<String, Object> inspectionque = inspectionques.get(0);
				String category = (String) inspectionque.get("category");
				if (StringUtil.isNotEmpty(category)) {
					category = category.replaceAll("<br>", "\\\n");
				}
				quarantine.setQuarantineCode((String) inspectionque.get("sj"));
				quarantine.setQuarantineType(category);
			}
		}

		// 3C认证
		Ccc ccc = new Ccc();
		List<Map<String, Object>> cccList = PubFun.requestInterface(
				interfaceConfig.getConfig("query_ccc") + "?language=0&hs=" + hscode, List.class);
		if (cccList != null && cccList.size() > 0) {
			Map<String, Object> cccMap = cccList.get(0);
			ccc.setType((String) cccMap.get("type"));
			ccc.setScope((String) cccMap.get("scope"));
		}

		Map<String, Object> importControls = new HashMap<String, Object>();
		importControls.put("regulatoryCondition", regulatoryCondition);
		importControls.put("quarantine", quarantine);
		importControls.put("ccc", ccc);
		viewMap.put("importControls", importControls);

		// 出口配额
		ExportAmount exportAmount = new ExportAmount();
		viewMap.put("exportAmount", exportAmount);
		List<Map<String, Object>> quotaList = PubFun.requestInterface(interfaceConfig.getConfig("query_quota") + "?hs=" + hscode,
				List.class);
		if (quotaList != null && quotaList.size() > 0) {
			Map<String, Object> quotaMap = quotaList.get(0);
			exportAmount.setName((String) quotaMap.get("commodity"));
			exportAmount.setAmount((String) quotaMap.get("quantity"));
			exportAmount.setGeneralTax((String) quotaMap.get("gen_out"));
			exportAmount.setMostFavNationsTax((String) quotaMap.get("mfn_out"));
			exportAmount.setAmountRate((String) quotaMap.get("duty"));
			exportAmount.setNationalityTax((String) quotaMap.get("newz"));
			exportAmount.setNationalityTaxRate((String) quotaMap.get("aus"));
		}

        // 判断是否关注过
        WmMemberAttention att = new WmMemberAttention();
        att.setType("5");
        att.setTypeCode(hscode);
        att.setMemberId(UserUtil.getCurrentMemberId());
        if(wmMemberAttentionService.judgeIfAttention(att)){
            viewMap.put("attention", "1");
        } else {
            viewMap.put("attention", "0");
        }

		return viewMap;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void getDir(String hscode, List<String> ret, Map<String, Object> source, int hslength) throws Exception {

		Object children = source.get("children");
		String id = (String) source.get("id");
		String text = (String) source.get("text");
		ret.add(id + " " + text);
		if (children != null && !hscode.equals(id)) {
			List<Map<String, Object>> childs = (List) children;
			if (childs.size() > 0) {
				if (hslength == 4) {
					for (Map<String, Object> child : childs) {
						if (hscode.length() == 4 && hscode.startsWith((String) child.get("id"))) {
							getDir(hscode, ret, child, hslength);
						}
					}
				} else {
					getDir(hscode, ret, childs.get(0), hslength);
				}
			}
		}
	}

}
